<?php

declare(strict_types=1);

namespace App\Controller;

use App\Job\CheckJob;
use App\Model\Order;
use App\Services\AdapayService;
use App\Services\FileService;
use App\Services\Iot\IotService;
use App\Services\Iot\IotService1;
use App\Services\QiniuService;
use App\Services\Signer;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\Context\ApplicationContext;
use Hyperf\Coroutine\Parallel;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Laminas\Stdlib\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use Util\Elasticsearch;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;

class TestController extends AbstractController
{

    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[inject]
    protected AdapayService $adapayService;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    public function test()
    {
        if (function_exists('pcntl_fork')) {
            echo "pcntl_fork 函数可用";
        } else {
            echo "pcntl_fork 函数不可用";
        }
        // return $qrCode->writeString();
//        $uri = 'weixin://wxpay/bizpayurl?pr=WFtWEEtzz';
//        $qrCode = QrCode::create($uri);
//        $writer = new PngWriter();
//        $result = $writer->write($qrCode);
//        return $result->getDataUri();
    }

    public function getNum(){
        IotService1::queryDeviceStatus('868488071723389');
    }

    public function turnUp(){
        IotService1::start('868488071723389',2,20,1);
    }

    public function readPower(){
        IotService1::readPower('868488071723389',2);
    }


    public function turnDown(){
        $signer = new Signer();
        $signUrl = $signer->getSigner();
        var_dump($signUrl);
    }

    public function readParams(){
        IotService1::readParams('868488071723389');
    }
    public function esDemo()
    {
        $es = new Elasticsearch();
//        搜索
        $a = $es->indexEs(['index' => 'test_books', 'id' => 3, 'body' => ['title' => '常年法律顾问服务合同', 'title_pic' =>'["\/upload\/img\/157\/0-157-64f44d3037f88.jpg","\/upload\/img\/157\/1-157-64f44d30e3fcb.jpg","\/upload\/img\/157\/2-157-64f44d31a3402.jpg","\/upload\/img\/157\/3-157-64f44d322ebea.jpg","\/upload\/img\/157\/4-157-64f44d32b3dcb.jpg","\/upload\/img\/157\/5-157-64f44d336334d.jpg","\/upload\/img\/157\/6-157-64f44d33c9d42.jpg","\/upload\/img\/157\/7-157-64f44d35363d6.jpg","\/upload\/img\/157\/8-157-64f44d35b3297.jpg","\/upload\/img\/157\/9-157-64f44d3671a0b.jpg","\/upload\/img\/157\/10-157-64f44d37214bc.jpg","\/upload\/img\/157\/11-157-64f44d37843e8.jpg"]']]);
        var_dump($a);
        $a = $es->search(['search' => ['title' => '法律合同'], 'index' => 'test_books'], 1, 5);
        var_dump($a);
        // 删除文档
//        $a = $es->deleteEs(['index' => 'books', 'type' => '_doc', 'id' => 3]);
//        var_dump($a);
//        // 按照id创建
//         $a = $es->indexEs(['index' => 'books', 'id' => 3, 'body' => ['title' => '常年法律顾问服务合同', 'title_pic' =>'["\/upload\/img\/157\/0-157-64f44d3037f88.jpg","\/upload\/img\/157\/1-157-64f44d30e3fcb.jpg","\/upload\/img\/157\/2-157-64f44d31a3402.jpg","\/upload\/img\/157\/3-157-64f44d322ebea.jpg","\/upload\/img\/157\/4-157-64f44d32b3dcb.jpg","\/upload\/img\/157\/5-157-64f44d336334d.jpg","\/upload\/img\/157\/6-157-64f44d33c9d42.jpg","\/upload\/img\/157\/7-157-64f44d35363d6.jpg","\/upload\/img\/157\/8-157-64f44d35b3297.jpg","\/upload\/img\/157\/9-157-64f44d3671a0b.jpg","\/upload\/img\/157\/10-157-64f44d37214bc.jpg","\/upload\/img\/157\/11-157-64f44d37843e8.jpg"]']]);
//         var_dump($a);
//         更新
//        $a = $es->update(['index' => 'books', 'id' => 4, 'doc' => ['title' => 1222223, 'name' => 11111]]);
//        var_dump($a);
    }

    public function pay_test(RequestInterface $request){

            $this->adapayService->Payment();
    }

    public function close_pay(){
            $this->adapayService->closePay();
    }
}
